import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

export default defineConfig(({ mode }) => { // 使用 mode 参数
  const env = loadEnv(mode, process.cwd(), ''); // 加载环境变量
  return {
    envPrefix: ['VITE', 'VUE'], // 环境变量前缀
    define: {
      'process.env.VITE_APP_BASE_API': JSON.stringify(env.VITE_APP_BASE_API || '/'), // 确保有默认值
    },
    build: {
      target: 'esnext', // 或 'es2022' 等支持 top-level await 的环境
    },
    server: {
      host: '0.0.0.0',
      port: 8080,
    },
    base: './', // 修改这里的值为您想要设置的新路径
    plugins: [
      vue(),
      vueDevTools(),
      VueSetupExtend(),
      AutoImport({
        resolvers: [ElementPlusResolver(), IconsResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver(), IconsResolver({
          prefix: false, // <--
          // this is optional, default enabling all the collections supported by Iconify
          enabledCollections: ['mdi'],
        })],
      }),
      Icons(
        {
          autoInstall: true,
        }
      ),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
  };
});